package com.cn.bukeManage.service.serviceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.cn.bukeManage.dao.IndentDao;
import com.cn.bukeManage.dao.OrdersDao;
import com.cn.bukeManage.dao.UserDao;
import com.cn.bukeManage.pojo.Customer;
import com.cn.bukeManage.pojo.Customer_address;
import com.cn.bukeManage.pojo.Dealer;
import com.cn.bukeManage.pojo.Indent;
import com.cn.bukeManage.pojo.User;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.service.IndentService;
import com.cn.bukeManage.utils.LuKeBeanUtil;

@Service
public class IndentServiceImpl implements IndentService {

	@Autowired 
	private IndentDao indentDao;
	@Autowired
	private OrdersDao ordersDao;
	@Autowired
	private UserDao userDao;
	
	
	

	
	public int getTotalPages(){
		Map<String, Object> map = new HashMap<>();
		map.put("phone", SecurityUtils.getSubject().getSession().getAttribute("userName"));
		map.put("status", 15);
		int totalRecords=indentDao.getAllRecords(map);
		//int totalPageNum = (totalRecords  +  CurtainConstant.PAGESIZE  - 1) / CurtainConstant.PAGESIZE;  
		return totalRecords;
	}
	
	
	
	@Override
	public List<Map<String,Object>> showGoods(HttpServletRequest request, Integer indentId) {
		User currentUser=userDao.getUserById(Integer.valueOf(String.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId"))));
		List<Map<String,Object>> goodsList=null;
		if(currentUser !=null){
			String vertification=currentUser.getVertification();
			goodsList= indentDao.selectGoodsOfMine(indentId);
			for(Map<String,Object> goodsMap : goodsList){
				switch (vertification) {
				case "1":
					goodsMap.put("myProfitRate", goodsMap.get("profitRateAgent"));
					break;
				case "2":
					goodsMap.put("myProfitRate", goodsMap.get("profitRateDeclar"));	
					break;
				case "3":
					goodsMap.put("myProfitRate", goodsMap.get("profitRatePromotor"));
					break;
				default:
					goodsMap.put("myProfitRate", 0.00);
					break;
				}    
			}                                                                                                                                                                                                          
		}
		return goodsList;
	}
	
	//根据安装测量维修订单的类型, 和状态返回他一 页的数据
	public List<Map<String, Object>> getIndentOfDataByTypeAndStatus(Model model,Integer currentPage, Integer type, Integer status) {
		Map<String, Object> maps=new HashMap<>();
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
		maps.put("type", type==0?-1:type);
		maps.put("status", status==0?-1:status);
		maps.put("phone", SecurityUtils.getSubject().getSession().getAttribute("userName"));
		//分页
		page.setRows(indentDao.selectMyIndentsInfo(maps).size());
		model.addAttribute("page",page);
		maps.put("begin", (currentPage-1)*page.getPageSize());
		maps.put("pageSize", page.getPageSize());
		List<Indent> indents=indentDao.selectMyIndentsInfo(maps);
		List<Map<String, Object>>list=new ArrayList<Map<String, Object>>();
		for(Indent indent:indents){
			//订单id,下单时间, 下单人, 地址联系人,下单地址,定金,客户预约时间,备注
			Map<String,Object> map=new HashMap<>();
			Map<String,Object>indentMap=LuKeBeanUtil.beanToMap(indent);
			if (indent.getCustomerId()!=null&&indent.getDealerId()==null){
				Customer customer = ordersDao.getCustomerByCustomerId(indent.getCustomerId());
				if(customer !=null ){
					if (customer.getPhone()==null) {
						indentMap.put("xiaDanRen",customer.getNickName()+"(子账号)");
					}else{
						indentMap.put("xiaDanRen",customer.getNickName()+"(顾客)");
					}
				}
			}else if (indent.getCustomerId()==null&&indent.getDealerId()!=null){
				Dealer dealer=ordersDao.getDealerByDealerId(indent.getDealerId());
				indentMap.put("xiadanRen",dealer.getName()+"(经销商)");
			}
			Customer_address address = ordersDao.getAddressByCuAdId(indent.getCuAdId());
			Map<String,Object> addressMap=LuKeBeanUtil.beanToMap(address);	
			addressMap.put("address",address.getCity()+address.getAddress());		
			map.putAll(indentMap);
			map.putAll(addressMap);
			list.add(map);		
		}
		return list;
	}
	
	

}
